<!DOCTYPE html>
<html lang="en">
 
 <head>
 <meta charset="UTF-8">
 <title>图片懒加载(可视区域加载)</title>
 <style>
  * {
  padding: 0px;
  margin: 0px;
  }
   
  html,
  body {
  width: 100%;
  min-height: 100%;
  }
   
  #SB {
  margin: 0;
  padding: 0;
  list-style: none;
  }
   
  #SB .in {
  border: 1px solid red;
  margin: 10px;
  text-align: center;
  height: 400px;
  width: 400px;
  float: left;
  }
   
  .in img {
  border: none;
  vertical-align: middle;
  height: 400px;
  width: 400px;
  }
 </style>
 </head>
 
 <body>
 <ul id="SB">
  <!--<li class="in"><img src="" data-imgurl="img/1.png"></li>
  <li class="in"><img src="" data-imgurl="img/2.png"></li>
  <li class="in"><img src="" data-imgurl="img/3.png"></li>
  <li class="in"><img src="" data-imgurl="img/4.png"></li>
  <li class="in"><img src="" data-imgurl="img/5.png"></li>
  <li class="in"><img src="" data-imgurl="img/6.png"></li>
  <li class="in"><img src="" data-imgurl="img/7.png"></li>
  <li class="in"><img src="" data-imgurl="img/8.png"></li>
  <li class="in"><img src="" data-imgurl="img/9.png"></li>
  <li class="in"><img src="" data-imgurl="img/10.png"></li>
  <li class="in"><img src="" data-imgurl="img/11.png"></li>
  <li class="in"><img src="" data-imgurl="img/12.png"></li>
  <li class="in"><img src="" data-imgurl="img/13.png"></li>
  <li class="in"><img src="" data-imgurl="img/14.png"></li>
  <li class="in"><img src="" data-imgurl="img/15.png"></li>
  <li class="in"><img src="" data-imgurl="img/16.png"></li>
  <li class="in"><img src="" data-imgurl="img/1.png"></li>
  <li class="in"><img src="" data-imgurl="img/2.png"></li>
  <li class="in"><img src="" data-imgurl="img/3.png"></li>
  <li class="in"><img src="" data-imgurl="img/4.png"></li>
  <li class="in"><img src="" data-imgurl="img/5.png"></li>
  <li class="in"><img src="" data-imgurl="img/6.png"></li>
  <li class="in"><img src="" data-imgurl="img/7.png"></li>
  <li class="in"><img src="" data-imgurl="img/8.png"></li>
  <li class="in"><img src="" data-imgurl="img/9.png"></li>
  <li class="in"><img src="" data-imgurl="img/10.png"></li>
  <li class="in"><img src="" data-imgurl="img/11.png"></li>
  <li class="in"><img src="" data-imgurl="img/12.png"></li>
  <li class="in"><img src="" data-imgurl="img/13.png"></li>
  <li class="in"><img src="" data-imgurl="img/14.png"></li>
  <li class="in"><img src="" data-imgurl="img/15.png"></li>
  <li class="in"><img src="" data-imgurl="img/16.png"></li>
  <li class="in"><img src="" data-imgurl="img/1.png"></li>
  <li class="in"><img src="" data-imgurl="img/2.png"></li>
  <li class="in"><img src="" data-imgurl="img/3.png"></li>
  <li class="in"><img src="" data-imgurl="img/4.png"></li>
  <li class="in"><img src="" data-imgurl="img/5.png"></li>
  <li class="in"><img src="" data-imgurl="img/6.png"></li>
  <li class="in"><img src="" data-imgurl="img/7.png"></li>
  <li class="in"><img src="" data-imgurl="img/8.png"></li>
  <li class="in"><img src="" data-imgurl="img/9.png"></li>
  <li class="in"><img src="" data-imgurl="img/10.png"></li>
  <li class="in"><img src="" data-imgurl="img/11.png"></li>
  <li class="in"><img src="" data-imgurl="img/12.png"></li>
  <li class="in"><img src="" data-imgurl="img/13.png"></li>
  <li class="in"><img src="" data-imgurl="img/14.png"></li>
  <li class="in"><img src="" data-imgurl="img/15.png"></li>
  <li class="in"><img src="" data-imgurl="img/16.png"></li>-->
 </ul>
 <script type="text/javascript">
 	var str = "";
 	for(var i = 1, len = 16; i <= len; i++) {
 		str += "<li class='in'><img src='' data-imgurl='img/" + i +".png'></li>"
 	}
 	console.log(str)
 	document.getElementById("SB").innerHTML = str;
  var aImages = document.getElementById("SB").getElementsByTagName('img'); //获取id为SB的文档内所有的图片
  loadImg(aImages);
  window.onscroll = function() { //滚动条滚动触发
  	//当滚动的时候,执行代码
  loadImg(aImages);
  };
  //getBoundingClientRect 是图片懒加载的核心
  function loadImg(arr) {
  for(var i = 0, len = arr.length; i < len; i++) {
//	console.log(arr[i].getBoundingClientRect().top)
//	console.log(document.documentElement.clientHeight)//637
//	console.log(arr[i].isLoad)//undefined
//	console.log(typeof(arr[i]))
//	arr[i]是一个对象，对象可以直接添加值，就是img标签
   if(arr[i].getBoundingClientRect().top < document.documentElement.clientHeight && !arr[i].isLoad) {
   	//现在只有前两张的位置比上面的小，前两个的arr[i].isLoad原本是没有值的，下面给定一个值，防止重复加载
   arr[i].isLoad = true; //图片显示标志位
   //arr[i].style.cssText = "opacity: 0;"; 
   //避免污染变量i
   //在for循环内部使用延时加载，必须封装一次
   (function(i) {
    setTimeout(function() {
    	console.log(arr[i].dataset)
    if(arr[i].dataset) { //兼容不支持data的浏览器
     aftLoadImg(arr[i], arr[i].dataset.imgurl);
    } else {
     aftLoadImg(arr[i], arr[i].getAttribute("data-imgurl"));
    }
    arr[i].style.cssText = "transition: 1s; opacity: 1;" //相当于fadein
    }, 500)
   })(i);
   }
  }
  }
 
  function aftLoadImg(obj, url) {
  var oImg = new Image();
  oImg.onload = function() {
   obj.src = oImg.src; //下载完成后将该图片赋给目标obj目标对象
  }
  oImg.src = url; //oImg对象先下载该图像
  }
 </script>
 </body>
 
</html>